﻿using System;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data;
using BEGDC.Utility.NHibernate;
using Castle.Services.Transaction;
using System.Collections;
using System.Xml;
using Castle.Facilities.NHibernateIntegration;

namespace BEGDC.PreContract.BLL
{
	[Transactional]
	public partial class Province: IProvince
	{
		private  ISessionFactory  factory;

        /// <summary>
        /// Initiate session factory.
        /// </summary>
        /// <param name="sessionFactory"></param>
        public Province(ISessionFactory sessionFactory)
        {
            this.factory = sessionFactory;
        }
		
		//Get by id
		[Transaction(TransactionMode.Requires)]
		public BEGDC.PreContract.DAL.Province GetProvince(System.Int32 provinceID)
		{
			BEGDC.PreContract.DAL.Province province = BEGDC.PreContract.DAL.Province.TryFind(provinceID);

            return province;
		}
		
		//Get by filter
		[Transaction(TransactionMode.Requires)]
		public DataTable GetProvince_DataTable(string filter)
		{
			using (ISession session = factory.OpenSession())
            {
				string strSql = "SELECT * FROM Province";
				if (filter.Trim() != string.Empty)
					strSql += " WHERE " + filter;

                DataTable dtSearch = DAO.ExecuteDataTable(session, strSql);

                return dtSearch;
            }
		}
		
		//Delete
		[Transaction(TransactionMode.Requires)]
		public void DeleteProvince(System.Int32 provinceID)
		{
			try
            {
				BEGDC.PreContract.DAL.Province province = BEGDC.PreContract.DAL.Province.TryFind(provinceID);
                province.Delete();
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("Province", string.Format("Error occured when deleting the Province, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Add
		[Transaction(TransactionMode.Requires)]
        public int AddProvince(System.String provinceCode, System.String provinceEName, System.String provinceCName, System.Int32 sortNumber, Nullables.NullableDateTime createDate, Nullables.NullableDateTime updateDate, System.Boolean active, System.Int32 clusterID)	
		{
			try
            {
				BEGDC.PreContract.DAL.Province province = new BEGDC.PreContract.DAL.Province();
				
				province.ProvinceCode = provinceCode;
				province.ProvinceEName = provinceEName;
				province.ProvinceCName = provinceCName;
				province.SortNumber = sortNumber;
				province.CreateDate = createDate;
				province.UpdateDate = updateDate;
				province.Active = active;
                province.ClusterID = clusterID;
				province.CreateAndFlush();
				
				return province.ProvinceID;
			}
			catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("Province", string.Format("Error occured when adding a new Province, error information: {0}.", ex.Message), ex);
            }
		}
		
		//Update
		[Transaction(TransactionMode.Requires)]
        public bool UpdateProvince(System.Int32 provinceID, System.String provinceCode, System.String provinceEName, System.String provinceCName, System.Int32 sortNumber, Nullables.NullableDateTime createDate, Nullables.NullableDateTime updateDate, System.Boolean active, System.Int32 clusterID)
		{
			try
            {
                BEGDC.PreContract.DAL.Province province = GetProvince(provinceID);

				province.ProvinceCode = provinceCode;
				province.ProvinceEName = provinceEName;
				province.ProvinceCName = provinceCName;
				province.SortNumber = sortNumber;
				province.CreateDate = createDate;
				province.UpdateDate = updateDate;
				province.Active = active;
                province.ClusterID = clusterID;
				province.Save();
				
				return true;
            }

            catch (Exception ex)
            {
                throw new BEGDC.Utility.ExceptionManagement.GDCException("Province", string.Format("Error occured when updating the Province, error information: {0}.", ex.Message), ex);
            }
		}
	}
}



